// sessionStorage 封装
const suncnStorage = (function myStorage(){
    const ms = 'suncnStorage';
    const storage = window.sessionStorage;
  
    return {
      test: () => !!storage,
      // 设置key-value
      setItem: (key, value) => {
        let data = storage.getItem(ms);
        if(!data){
          this.init();
          data = storage.getItem(ms);
        }
        data = JSON.parse(data);
        data[key] = value;
        storage.setItem(ms,JSON.stringify(data));
        return data;
      },
      // 根据key读取value
      getItem: (key) => {
        let data = storage.getItem(ms);
        if(!data) return false;
        data = JSON.parse(data);
        return data[key];
      },
      // 移除指定key
      removeItem: (key) => {
        let data = storage.getItem(ms);
        if(!data) return false;
        data = JSON.parse(data);
        delete data[key];
        storage.setItem(ms,JSON.stringify(data));
        return data;
      },
      init: () => storage.setItem(ms,'{}'),
      clear: () => storage.removeItem(ms)
    };
  })();
  
  export default suncnStorage;